import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class FileIOMain {

	private ArrayList outputList = new ArrayList();
	
	FileIOMain()
	{
		outputList.add("学号：17200135129");
		outputList.add("姓名：唐啸虎");
		outputList.add("我爱运动：吃饭");
		for (int i = 0; i < 1000; i++)
		{
			String morning ="Good Morning";
			outputList.add(morning + i);
		}
	}
	
	public void readFile(String file_path) throws IOException
	{
		File file;
		FileReader fr;
		BufferedReader br;

		file = new File(file_path);
		if (!file.exists()){
			System.out.println("\""+file_path+"\" does not exsit!");
			return;
		}
		
		/*
		1.FileReader����java.io.InputStreamReade��չ���ģ�������ļ���ȡ�ģ�ֻ�ܽ��еײ���ֽڲ����� 
		BufferedReader��Reader����չ�������ṩͨ�õĻ��巽ʽ�ı���ȡ�������ṩ�˺�ʵ�õ�readLine����ȡ�����ı����ʺϣ�BufferedReader�����Reader�ģ���ֱ������ļ���Ҳ����ֻ����ļ���ȡ��  
		2.FileReader���������ļ����࣬��BufferReader�ǽ�IO��ת��ΪBuffer����߳���Ĵ����ٶȡ�
		*/
		fr = new FileReader(file);
		br = new BufferedReader(fr);
		
		/*��ҵ����д���´��룬ʹ��ѭ����ȡ�ļ�ȫ�����ݲ���ӡ����Ļ����ʾ����Ҫʹ��BufferedReader.ready()�������ж��ļ������Ƿ��ļ�ĩβ*/
		/*String lineText;
		lineText = br.readLine();
		System.out.println(lineText);
		lineText = br.readLine();
		System.out.println(lineText);
		lineText = br.readLine();
		System.out.println(lineText);*/
		String lineText;
		while (br.ready()){
			lineText = br.readLine();
			System.out.println(lineText);
		}
		/*��ҵend*/
		
		br.close();
	}

	public void writeFile(String file_path) throws IOException
	{
		File file;
		FileWriter fw;
		PrintWriter pw;

		file = new File(file_path);
		if (!file.exists()){
			file.createNewFile();
		}
		fw = new FileWriter(file);
		pw = new PrintWriter(fw);

		/*作业：改写以下代码，使用循环输出FileIOMain.outputList至文件*/
		for(int i=0;i<outputList.size();i++){
			String lineText;
			lineText = (String) outputList.get(i);
			pw.println(lineText);
		}
		/*String lineText;
		lineText = "学号：17200135129";
		pw.println(lineText);
		lineText = "姓名：唐啸虎";
		pw.println(lineText);
		lineText = "运动：吃饭";
		pw.println(lineText);*/
		/*作业end*/
		
		pw.close();
	}
	
	public static void main(String[] args) throws IOException {
		//D:\ideaWorkSpace\file_io\src
		FileIOMain io = new FileIOMain();
		io.readFile("D:\\ideaWorkSpace\\file_io\\src\\hw3_input.txt");
		io.writeFile("D:\\ideaWorkSpace\\file_io\\src\\hw3_output.txt");
	}

}
